package proyecto.struts.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import proyecto.struts.bean.Equipo;
import proyecto.struts.bean.Solicitud;
import proyecto.struts.dao.SolicitudDAO;
import proyecto.struts.util.MySqlDbConn;

public class SqlSolicitudDAO implements SolicitudDAO {

	@Override
	public List<Solicitud> buscarSolicitud(int id, String tarj)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int edita(Solicitud bean) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int elimina(Solicitud bean) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Solicitud getSolicitud(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int inserta(Solicitud bean) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Solicitud> buscarSolicitud(String representante, int idEstado)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

/*
	public int inserta(Solicitud bean) throws Exception {
		int rpta = 0; 
		System.out.println("llego al DAO inserta solicitud");
		Connection cn = MySqlDbConn.obtenerConexion();
		String sql = "INSERT INTO tb_solicitud values(null,?,?,?,?,?,?)";
		PreparedStatement stm = cn.prepareStatement(sql);
		try {
			
			stm.setString(1, bean.getDesc());
			stm.setString(2, bean.getRepresentante());
			stm.setString(3, bean.getFecha());
			stm.setString(4, bean.getHorometro());
			stm.setString(5, "V");
			stm.setString(6, bean.getNroTarjeta());
			stm.executeUpdate();
			
			stm.close();
			cn.close();
			
			
		} catch (SQLException e) {
			e.printStackTrace();			
			rpta = 1; 
		}finally{
			try {
				stm.close();
				cn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rpta;
	}

	public List buscarSolicitud(String id, String tarj) throws Exception {

		Solicitud bean = null;
		Connection cn = MySqlDbConn.obtenerConexion();
		Statement stm = cn.createStatement();
		String sql = " SELECT * FROM tb_solicitud Where ";
		if(!id.equals(""))
			sql = sql + " id_solicitud like '" + id + "' AND ";
		sql = sql + " tb_equipo_nrotarjeta like '%" + tarj + "%' ";
		System.out.println(sql);
		ResultSet rst = stm.executeQuery(sql);
		ArrayList lista = new ArrayList();
		
		while(rst.next()) {
			bean =  new Solicitud();
			bean.setId(rst.getString(1));
			bean.setDesc(rst.getString(2));
			bean.setRepresentante(rst.getString(3));
			bean.setFecha(rst.getString(4));
			bean.setHorometro(rst.getString(5));
			bean.setEstado(rst.getString(6));
			bean.setNroTarjeta(rst.getString(7));
			lista.add(bean);
		}

		rst.close();
		stm.close();
		cn.close();

		return lista;
	}

	public Solicitud getSolicitud(String id) throws Exception {
		Solicitud bean = null;
		Connection cn = MySqlDbConn.obtenerConexion();
		String sql = "SELECT * FROM tb_solicitud Where id_solicitud = ?";
		PreparedStatement stm = cn.prepareStatement(sql);
		stm.setString(1, id);
		ResultSet rst = stm.executeQuery();
		
		if (rst.next()) {
			bean =  new Solicitud();
			bean.setId(rst.getString(1));
			bean.setDesc(rst.getString(2));
			bean.setRepresentante(rst.getString(3));
			bean.setFecha(rst.getString(4));
			bean.setHorometro(rst.getString(5));
			bean.setEstado(rst.getString(6));
			bean.setNroTarjeta(rst.getString(7));
		}

		rst.close();
		stm.close();
		cn.close();

		return bean;
	}

	@Override
	public int edita(Solicitud bean) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int elimina(Solicitud bean) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}
*/
}
